﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Bon.It.Proxy;
using Bon_It.Model;

namespace Bonit_It.ViewModel
{
    public class UserViewModel : Base.ViewModel
    {
        /// <summary>
        /// the current User item.
        /// </summary>
        public Users User { get; set; }

        /// <summary>
        /// The <see cref="Groups"/> from the database.
        /// </summary>
        public Groups[] UserGroups { get; set; }


        public UserViewModel()
        {
            User= new Users();
            RefreshLookups();
            DisplayName = string.Format("Benutzer");
        }


        public UserViewModel(Users user) : this()
        {
            Debug.Assert(user != null , "User cannot be null!");
            User = user;
        }

        #region Overrides of ViewModel<Users>


       


        public override bool CanDelete(object argument)
        {
            return false;
        }

        public override bool CanSave(object argument)
        {
            if (User!= null)
            {
                return string.IsNullOrEmpty(User.Error);
            }
            return false;
        }

        public override bool CanCreate(object argument)
        {
            return false;
        }


        public override void OnSave(object argument)
        {
            if (User != null)
            {
                ServiceProxy.Instance.PersistUser(User);   
                AfterSave();
            }
        }

       
        public override void RefreshLookups()
        {
            UserGroups = ServiceProxy.Instance.GetGroups().ToArray();
        }

        public override void CleanUp()
        {
            User = null;
            UserGroups = null;
        }

        #endregion
    }
}
